// ==UserScript==
// @name         获取天猫价格信息
// @namespace    http://tampermonkey.net/
// @version      2024-10-25
// @description  try to take over the world!
// @author       You
// @match        https://detail.tmall.com/item.htm?*
// @icon         http://wuweijun.fun/favicon.ico
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  let oLi = document.createElement("div");
  oLi.innerHTML = `
	<div style="
		position: absolute;
		top: 146px;
		left: 246px;
		background-color: #7ec1da;
		z-index: 9999;
		display: flex;
    align-items: center;"  >
      <div id="jiagehuoqu" style="color: #fbefef; cursor: pointer; display: inline; width: 41px; text-align: center;">价格</div>
      <textarea id="textarea22"></textarea>
    </div>`

  let body2 = document.body;

  body2.appendChild(oLi)

  let jhl
  let gg
  let jiage
  let hjls
  let ggs
  let s
  function r() {
    // 净含量
    jhl = document.querySelector('.SkuContent--HmMiDKFt').childNodes[0];
    // 规格
    gg = document.querySelector('.SkuContent--HmMiDKFt').childNodes[1];
    // 价格
    jiage = document.querySelectorAll('.displayPrice--oD54EJg0 .subPrice--PpThOLBO .text--fZ9NUhyQ');

    hjls = document.querySelector('.SkuContent--HmMiDKFt').childNodes[0].querySelectorAll('.valueItemText--HiKnUqGa');
    ggs = document.querySelector('.SkuContent--HmMiDKFt').childNodes[1].querySelectorAll('.valueItemText--HiKnUqGa');

    s = '';

    // 清空已经选择的
    document.querySelectorAll('.isSelected--YrA6x4Yj').forEach(item => { item.click() })
    // 默认选择第一个净含量
    hjls[0].click();

    run()
  }

  function run() {
    let hl = hjls.length;
    let gl = ggs.length;
    let ht = 1;
    let gt = 0;
    let flag = false;
    let t = setInterval(() => {
      if (!flag && gt < gl) {
        ggs[gt++].click();
        setTimeout(() => {
          // console.log(`${hjls[ht - 1].innerText}  ${ggs[gt - 1].innerText}  ${jiage[1].innerText}元\n`)
          s += `${hjls[ht - 1].innerText}  ${ggs[gt - 1].innerText}  ${jiage[1].innerText}元\n`
        },90);
      } else {
        flag = true;
        gt = 0;
      }

      if (flag && ht < hl) {
        hjls[ht++].click();
        flag = false;
      } else if (flag) {
        // copyText(s);
        console.log(s)
        document.getElementById('textarea22').value = s;
        clearInterval(t);
      }
    }, 700)
  }

  document.getElementById('jiagehuoqu').onclick = r;

  // 复制文本内容
  function copyText(text) {
    let inputDom = document.createElement('input');  // 创建一个input元素
    // let inputDom = document.createElement('textarea');  // 创建一个input元素
    inputDom.setAttribute('readonly', 'readonly'); // 防止手机上弹出软键盘
    inputDom.value = text; // 给input元素赋值
    document.body.appendChild(inputDom); // 添加到body
    inputDom.select(); //选中input元素的内容
    document.execCommand('Copy'); // 执行浏览器复制命令
    inputDom.style.display = 'none';
    inputDom.remove(); // 移除input元素
  }

  // Your code here...
})();